package com.itheima.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.ReportDao_Lizekai;
import com.itheima.entity.Report;
import com.itheima.pojo.Setmeal;
import com.itheima.service.ReportService_Lizekai;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = ReportService_Lizekai.class)
@Transactional
public class ReportServiceImpl_Lizekai implements ReportService_Lizekai {
    @Autowired
    private ReportDao_Lizekai reportDao;
    @Override
    public int orderCount(String format) {
        return reportDao.orderCount(format);
    }

    @Override
    public int orderVisitCount(String format) {
        return reportDao.orderVisitCount(format);
    }

    @Override
    public List<Map<String, Object>> getSetmealPriceReport(String start, String last) {
        List<Setmeal> setmealList = reportDao.findSetmealName();
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (Setmeal s : setmealList) {
            Map<String, String> map = new HashMap<>();
            map.put("startTime",start);
            map.put("lastTime",last);
            map.put("setmealName",s.getName());
            int count = reportDao.findCountOrderMember(map);
            Map<String, Object> map1 = new HashMap<>();
            map1.put("name",s.getName());
            int sum = (int) (count*s.getPrice());
            map1.put("value",sum);
            list.add(map1);
        }
        return list;
    }

    @Override
    public List<Map<String, Object>> getMemberAgeReport() throws Exception {
        //0-6
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(new Date());
        rightNow.add(Calendar.YEAR, -6);
        Date zero_six = rightNow.getTime();
        String six = DateUtils.parseDate2String(zero_six);
        String zero = DateUtils.parseDate2String(new Date());
        //婴幼儿会员人数
        int toddlerCount = reportDao.findToddler(zero,six);
        //7-12
        rightNow.setTime(new Date());
        rightNow.add(Calendar.YEAR, -12);
        Date seven_twelve = rightNow.getTime();
        String twelve = DateUtils.parseDate2String(seven_twelve);
        int childrenCount = reportDao.findChildren(six,twelve);
        //13-17
        rightNow.setTime(new Date());
        rightNow.add(Calendar.YEAR, -17);
        Date twelve_Seventeen = rightNow.getTime();
        String seventeen = DateUtils.parseDate2String(twelve_Seventeen);
        int teensCount = reportDao.findTeens(twelve,seventeen);
        //18-45
        rightNow.setTime(new Date());
        rightNow.add(Calendar.YEAR, -45);
        Date eighteen_Forty_five= rightNow.getTime();
        String forty_five= DateUtils.parseDate2String(eighteen_Forty_five);
        int youthCount = reportDao.findYouth(seventeen,forty_five);
        //46-69
        rightNow.setTime(new Date());
        rightNow.add(Calendar.YEAR, -69);
        Date forty_six_Sixty_nine = rightNow.getTime();
        String sixty_nine= DateUtils.parseDate2String(forty_six_Sixty_nine);
        int middleAgedCount = reportDao.findMiddleAged(forty_five,sixty_nine);
        //>69
        int elderlyCount = reportDao.findElderly(sixty_nine);
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map1 = new HashMap<>();
        map1.put("name","婴幼儿");
        map1.put("value",toddlerCount);
        list.add(map1);
        Map<String, Object> map2 = new HashMap<>();
        map2.put("name","少儿");
        map2.put("value",childrenCount);
        list.add(map2);
        Map<String, Object> map3 = new HashMap<>();
        map3.put("name","青少年");
        map3.put("value",teensCount);
        list.add(map3);
        Map<String, Object> map4 = new HashMap<>();
        map4.put("name","青年");
        map4.put("value",youthCount);
        list.add(map4);
        Map<String, Object> map5 = new HashMap<>();
        map5.put("name","中年");
        map5.put("value",middleAgedCount);
        list.add(map5);
        Map<String, Object> map6 = new HashMap<>();
        map6.put("name","老年");
        map6.put("value",elderlyCount);
        list.add(map6);
        return list;
    }

    @Override
    public Report getMemberSexReport() {
        Report report = new Report();
        List<Integer> manList = new ArrayList<>();
        List<Integer> womanList = new ArrayList<>();
        List<String> yearList = new ArrayList<>();
        //60后
        int afterSixtyMan = reportDao.findafterSixtyManCount();
        int afterSixtyWoman = reportDao.findafterSixtyWomanCount();
        manList.add(afterSixtyMan);
        womanList.add(afterSixtyWoman);
        yearList.add("60后");
        //70后
        int afterSeventyMan = reportDao.findafterSeventyManCount();
        int afterSeventyWoman = reportDao.findafterSeventyWomanCount();
        manList.add(afterSeventyMan);
        womanList.add(afterSeventyWoman);
        yearList.add("70后");
        //80后
        int afterEightyMan = reportDao.findafterEightyManCount();
        int afterEightyWoman = reportDao.findafterEightyWomanCount();
        manList.add(afterEightyMan);
        womanList.add(afterEightyWoman);
        yearList.add("80后");
        //90后
        int afterNinetyMan = reportDao.findafterNinetyManCount();
        int afterNinetyWoman = reportDao.findafterNinetyWomanCount();
        manList.add(afterNinetyMan);
        womanList.add(afterNinetyWoman);
        yearList.add("90后");
        //00后
        int afterZeroMan = reportDao.findafterZeroManCount();
        int afterZeroWoman = reportDao.findafterZeroWomanCount();
        manList.add(afterZeroMan);
        womanList.add(afterZeroWoman);
        yearList.add("00后");
        //10后
        int afterTenMan = reportDao.findafterTenManCount();
        int afterTenWoman = reportDao.findafterTenWomanCount();
        manList.add(afterTenMan);
        womanList.add(afterTenWoman);
        yearList.add("10后");
        report.setMan(manList);
        report.setWoman(womanList);
        report.setYear(yearList);
        return report;
    }
}
